/**
 * @file getMail.c
 * @author Xiangrui Meng & Yuqi Liu
 * @brief 传入邮件，把邮件具体信息中的header和content分开以便作base64转码处理
 * @return num=1 return header,num =2 return content_plain0, num=3 return content_html.
 * @version 0.1
 * @date 2022-06-3
 *
 * @copyright Copyright (c) 2022
 *
 */

// 从server拿到指定邮件的全部信息
char *divMail(char *recTemp0, int num);
char rec0[4096];
char header0[4096];
char content_plain0[4096];
char content_html0[4096];

char *divMail(char *recTemp0, int num)
{
    
    memset(rec0, 0, strlen(rec0));
    memset(header0, 0, strlen(header0));
    memset(content_plain0, 0, strlen(content_plain0));
    memset(content_html0, 0, strlen(content_html0));
    // 拿到具体邮件的全部内容

    strcpy(rec0, recTemp0);
    //printf("%s", rec0);

    char seg[] = "\r\n";
    char *token;

    int count = 0;
    token = strtok(rec0, seg);
    while ((token = strtok(NULL, seg)) != NULL)
    {
        // read header0s
        if (strncmp(token, "------=_Part_", strlen("------=_Part_")) != 0 && count == 0)
        {
            strcat(header0, token);
            strcat(header0, "\r\n");
        }
        // read content,type:plain
        else if (strncmp(token, "------=_Part_", strlen("------=_Part_")) != 0 && count == 1)
        {
            strcat(content_plain0, token);
            strcat(content_plain0, "\r\n");
        }
        // read content,type:html
        else if (strncmp(token, "------=_Part_", strlen("------=_Part_")) != 0 && count == 2)
        {
            strcat(content_html0, token);
            strcat(content_html0, "\r\n");
        }
        else
        {
            count++;
        }
    }


    if (num == 1)
    {
        return header0;
    }
    else if (num == 2)
    {
        return content_plain0;
    }
    else if (num == 3)
    {
        return content_html0;
    }
}
